package practica1.actions;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import practica1.Practica1;
import practica1.controller.Controller;
import practica1.manejoExperimento.ImprimirExperimento;
import practica1.language.Language;
import practica1.language.LanguageEvent;
import practica1.language.LanguageListener;
import practica1.logic.LogicExperimento;

/**
 * Clase que implementa la acción para imprimir un experimento
 * @author Miguel González - Ceura
 */
public class ActionImprimir extends AbstractAction implements LanguageListener {
    
    /**
     * Constructor de la clase
     */
    public ActionImprimir() {
        super(Language.getI().getP("IMPRIMIR"), null);

        Language.getI().addLanguageListener(this);
        
        putValue(SHORT_DESCRIPTION, Language.getI().getP("IMPRIMIR_EXPERIMENTO"));
        putValue(NAME, Language.getI().getP("IMPRIMIR"));
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I, 
                InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
        putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_I));
        putValue(SELECTED_KEY, Boolean.TRUE);
    }
    
    /**
     * Cuando sucede la acción nos encargamos de imprimir el experimento principal
     * @param e ActionEvent Evento producido
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        LogicExperimento exp = Controller.getInstance().
                getExperimentoPrincipal();
        
        if(exp != null) {
            try {
                ImprimirExperimento.imprimir(exp);
            } catch (RuntimeException ex) {
                JOptionPane.showMessageDialog(Practica1.getInstance(),
                            Language.getI().getP("CANCELADO_IMPRESION"), 
                            Language.getI().getP("ERROR"),
                                    JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(Practica1.getInstance(),
                        Language.getI().getP("SELECCIONA_EXPERIMENTO_COMO"
                    + "_PRINCIPAL"), Language.getI().getP("CERRAR"),
                                JOptionPane.WARNING_MESSAGE);
        }
    }

    /**
     * Al cambiar el idioma actualizamos el nombre y la descripción
     * @param evt LanguageEvent
     */
    @Override
    public void idiomaChange(LanguageEvent evt) {
        putValue(SHORT_DESCRIPTION, Language.getI().getP("IMPRIMIR_EXPERIMENTO"));
        putValue(NAME, Language.getI().getP("IMPRIMIR"));
    }
}
